/**
 * Created by huangxuehua on 2017/11/24.
 */
//$("header>a.goBack").click(function(){
//    window.location.href="../../my_center.html"
//})
var toastLoading = new auiToast({});
var xpHome = new Vue({
    el:"#app",
    data:{
        imgBaseUrl:nUrls.imgBaseUrl,
        indentList:[],//订单列表
        countPrice:[],//合计金额
        countNum:[],//产品总数
        tuijianrenInfo:[]//推荐人
    },
    methods:{
        getImage:function(url){//加载图片  处理 url为空 加载出错
            if(url){
                return xpHome.imgBaseUrl+url;
            }
        },
        getDate:function(time){
            return nUtils.dateTime(time);
        },
        changeTab:function(status){//切换
            xpHome.getOrderInfo(status);
        },
        getOrderInfo: function (status) {
            toastLoading.loading({
                title:"数据加载中",
            },function(ret){
            });
            var data = {
                "userid": userInfo.userid,
                "access_token": userInfo.access_token,
                "data[status]":status
            }
            $.ajax({
                url: nUrls.getIndentInfo,
                type: 'POST',
                data: data,
                success: function (data) {
                    //console.log(data)
                    var result = eval('(' + data + ')');
                    if (result && result.code == 200) {
                        xpHome.indentList = result.data;
                        //console.log("======"+status,xpHome.indentList);
                        xpHome.countMoneyFun();
                        toastLoading.hide();
                    }
                },
                error: function () {
                    console.log("获取信息出错");
                    toastLoading.hide();
                }
            })
        },
        getTypeAndColor:function(target,type){   //分割字符串
            var temp = "";
            if(target){
                var arr = target.split("-----");
                if(arr.length == 2){
                    if(type == 0){//颜色
                        return arr[1];
                    }else{//型号
                        return arr[0];
                    }
                }
            }
            return temp;
        },
        gotiXiang:function(idx){//提醒发货
            var data={
               "data[order_id]":xpHome.indentList[idx].order_id,
            }
            $.ajax({
                url:nUrls.getTiXing,
                type:"POST",
                data:data,
                success:function(data){
                    var result = eval('(' + data + ')');
                    if (result && result.code == 200) {
                        var toast = new auiToast(); //初始化toast事件
                        toast.custom({
                            title: "已提醒商家发货",
                            html: '',
                            duration: 2000
                        })
                        return;

                    }
                },
                error: function () {
                    console.log("提醒出错")
                }
            })

        },
        countMoneyFun:function(){//合计         一个订单一次计算
            for(var i = 0; i < xpHome.indentList.length; i++){//循环订单
                xpHome.countPrice[i] = 0;
                xpHome.countNum[i] = 0;
                var temp=xpHome.indentList[i];
                var tempChildren = temp.children;

                var beforeUserInfo = "";// 推荐人信息添加
                var userInfo = {isOneUser:true,oneUserName:"",refereName:[]};// 推荐人信息添加

                for(var j=0;j<tempChildren.length;j++){//循环订单中的物品
                    xpHome.countPrice[i] += temp.children[j].shop_price * temp.children[j].goods_number;
                    xpHome.countNum[i] += parseInt(temp.children[j].goods_number);

                    if(j==0){ //推荐人信息添加
                        beforeUserInfo = temp.children[j].refere_name;
                    }
                    userInfo.oneUserName = temp.children[j].refere_name;
                    userInfo.refereName.push(userInfo.oneUserName);
                    if(beforeUserInfo!= temp.children[j].refere_name){
                        userInfo.isOneUser = false;
                    }
                }
                xpHome.tuijianrenInfo[i] = userInfo;
            }
            //console.log("6666:",xpHome.tuijianrenInfo);
        },
        goOrder_detail:function(idx){  //跳转订单详情页
               //console.log("--------"+idx,xpHome.indentList[idx])
            var tUrl = "../../order_detail/order_detail.html";
            //// status //待付款 unpayed  //待发货 unshipped  //待收货 unchecked  //待评价 uncomment
            window.location.href = tUrl;
            window.localStorage.setItem("IndentGoodsDetail",JSON.stringify(xpHome.indentList[idx]));
        },
        cancelIndent:function(idx){
            var dialog = new auiDialog();
            dialog.alert({
                title:"温馨提示",
                msg:"确认取消此商品",
                buttons:['取消','确定']
            },function(ret){
                if(ret.buttonIndex == 2){//确定
                    var data={
                        "data[order_id]":xpHome.indentList[idx].order_id,//订单Id
                        "access_token":userInfo.access_token,
                    }
                    //console.log(data)
                    $.ajax({
                        url:nUrls.getCancelIndent,
                        type:"POST",
                        data:data,
                        success:function(data){
                            var result = eval('(' + data + ')');
                            if (result && result.code == 200) {
                                xpHome.indentList.splice(idx,1);
                            }
                        },
                        error: function () {
                            console.log("取消支付出错");
                        }
                    })
                }
            })
        },
        goBuy:function(idx){//去支付
            for(var i=0;i<xpHome.indentList.length;i++) {
                var goods_amount = xpHome.indentList[idx].goods_amount;
                var order_sn = xpHome.indentList[idx].order_sn;
                window.location.href="http://www.pooai.cn/wxpay/example/jsapi.php?tolfee="+goods_amount+"&ordersn="+order_sn;
            }
            //console.log(xpHome.indentList[idx].order_sn)

        },
        delIndent:function(idx){   //删除订单
            var dialog = new auiDialog();
            dialog.alert({
                title:"温馨提示",
                msg:"确认取消此产品",
                buttons:['取消','确定']
            },function(ret){
                if(ret.buttonIndex == 2){//确定
                    var data={
                        "data[order_id]":xpHome.indentList[idx].order_id,//订单Id
                    }
                    //console.log(data)
                    $.ajax({
                        url:nUrls.getDelIndent,
                        type:"POST",
                        data:data,
                        success:function(data){
                            var result = eval('(' + data + ')');
                            if (result && result.code == 200) {

                            }
                        },
                        error: function () {
                            console.log("删除订单出错");
                        }
                    })
                }
            })

        },
        goConfirmGoods:function(idx){//确认收货
            var dialog = new auiDialog();
            dialog.alert({
                title:"温馨提示",
                msg:"确认收货",
                buttons:['取消','确定']
            },function(ret){
                if(ret.buttonIndex == 2){//确定
                    var data={
                        "access_token":userInfo.access_token,
                        "data[order_id]":xpHome.indentList[idx].order_id,//订单Id
                    }
                    $.ajax({
                        url:nUrls.getConfirmGoods,
                        type:"POST",
                        data:data,
                        success:function(data){
                            var result = eval('(' + data + ')');
                            if (result && result.code == 200) {
                             //console.log("99999"+result.data)
                                window.location.href="./confirmGoods.html";
                                window.localStorage.setItem("IndentGoodsInfo",JSON.stringify(xpHome.indentList[idx]));
                            }
                        },
                        error: function (msg) {
                            console.log("确认收货出错",msg);
                        }
                    })
                }
            })
        },
        goComment:function(idx){//去评论
            window.localStorage.setItem("commentGoodsInfo",JSON.stringify(xpHome.indentList[idx]));
            window.location.href="../../order_detail/commentDistrict.html";
        },
        getOrderStatus:function(order,comment_status){//根据 order 获取 订单状态名称
            var status = "";
            switch (order){
                case "0":
                    status = "待付款";
                break;
                case "1":
                    status = "待发货";
                break;
                case "2":
                    status = "待收货";
                    break;
                case "3":
                        status = "待评价";
                    break;
                case "4":
                    status = "已评价";
                    break;

            }
            return status;
        } ,
        goWuliu:function(idx,flag,orderId){

            if(flag=="unchecked") {//待收货
                window.location.href = "../wuliu.html?flag=unchecked&orderId=" + orderId;
            }
            //}else if(flag == "unshipped"){//代发货
            //    window.location.href = "../wuliu.html?flag=unshipped&orderId="+orderId;
            //}
        },
        goOnLine:function(){  //断网刷新
            nUtils.onLine();
        }

    }
});

$(function(){
    var flag = nUtils.getUrlParam("flag");
    var indexTab = 1;
    //console.log("=====flag===",flag);
    switch (flag){
        case "all":
            xpHome.getOrderInfo('all');//获取全部
            indexTab =1;
         break;
        case "unpayed":
            xpHome.getOrderInfo('unpayed');
            indexTab = 2;
            break;
        case "unshipped":
            xpHome.getOrderInfo('unshipped');
            indexTab = 3;
            break;
        case "unchecked":
            xpHome.getOrderInfo('unchecked');
            indexTab = 4;
            break;
        case "uncomment":
            xpHome.getOrderInfo('uncomment');
            indexTab = 5;
            break;
        default:
            break;
    }

    var tab = new auiTab({//切换页面
        element:document.getElementById("tab"),
        index:indexTab,
        repeatClick:false
    },function(ret){
        //console.log(ret);
    });

})




